package com.jnsw.testkotlin

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.ListView
import android.widget.SimpleAdapter
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_list.*
import java.util.*

class List1Activity : AppCompatActivity() {

    var lmap = ArrayList<Map<String, String>>()
    var map = HashMap<String, String>()
    var simpleAdapter:SimpleAdapter? = null;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_list)

       var username =  intent.getStringExtra("username");

        tv_title.text = username // findviewbyid    setText

        println( "UserName:$username")
        var x = 0;
        while (x < 6) {
            x++
            map = HashMap<String, String>()
            map.put("title", x.toString()+"title")
            lmap.add(map)
        }
        bttn.setOnClickListener { startActivity(Intent(applicationContext,ImageSActivity::class.java)) }
        var list = findViewById(R.id.list) as ListView
         simpleAdapter    = SimpleAdapter(baseContext, lmap, android.R.layout.activity_list_item, arrayOf("title"), intArrayOf(android.R.id.text1))
//        simpleAdapter!!.filter.filter("2");
        list.adapter = simpleAdapter!!
        list.setOnItemClickListener { parent, view, position, id ->
            var title = lmap.get(position).get("title")
            Toast.makeText(getApplication(), "the clickitem title  is $title", Toast.LENGTH_SHORT).show()   //字符串模板

            if ((position == 2) and !title.isNullOrEmpty()) {
                Toast.makeText(getApplication(), "当前点击第二项", Toast.LENGTH_SHORT).show()
            }
            useWhen(position)
            startActivity(Intent(applicationContext,RecycleActivity::class.java));
        }
        btn.setOnClickListener {
            var str = et.text;
            simpleAdapter!!.filter.filter(str);
            simpleAdapter!!. notifyDataSetInvalidated();

        }

    }

    fun useWhen(input: Int) {
        var result = when (input) {
        //判断input是否为1
            1 -> {
                println("input == 1")
                 "1"
            }
        //判断input是否为2
            2 -> {
                println("input == 2")
                 "2"
            }
        //input是否在10到20的范围内
            in 10..20 -> {
                println("input number in the range")
                 "10-20"
            }
        //input是否不在20到30的范围内
            in 20..30 -> {

                println("input number outside the range")
                 "20-30"
            }
            else -> {
                //When作为表达式使用时，最后一定要以else
                println("input: $input")
                 "else  :  $input"
            }
        }
        println("result: $result")
    }
}
